/******************************************************文本框*****************************************************/
_$.CheckBoxList=function (target){
    var _this = this;
    _$.CheckBoxList.superclass.constructor.call(_this,target);
};
_$.regClass("CheckBoxList",_$.CheckBoxList);

_$.extend(_$.CheckBoxList,_$.FormItem, {
    _cls:_$._clsPre + "CheckBoxList",
    _uiCls:_$._uiPreCls + "CheckBoxList",
    _attrProps:function(){
        var _this = this;
        var properties = _$.CheckBoxList.superclass._attrProps.call(_this);
        properties.addAll(["onclick","onchange","idField","textField","resultKey",{data:'object',"dataFilter":"function"}]);
        return properties;
    },
    _attrOpts:function(){
        var _this = this;
        var opts = _$.CheckBoxList.superclass._attrOpts.call(_this);
        return $.extend(true,opts,{
            "idField":"id",
            "textField":"name",
            "basePath":"/base",
            "apiServer":"base",
            "call":"",
            "dataFilter":function(list){
                return list;
            }
        });
    },
    _editModel:function(){
        var _this = this;
        _this._clearShow();

        var source='<div  class="CheckBoxList"></div>';
        _this._replaceNode(source);
        _this.component = $(".CheckBoxList",_this.el);
        _this._initCheckBoxList();
        _this.el.addClass(_this._uiCls);
        _this._setStyle();
        _this.setOption("model",_EDITABLE_);
        _this._init();
        _this._bindEvents();
    },
    _initCheckBoxList:function(){
        var _this = this;
        _this._loadData(_this._editInit);
    },
    _init:function(){
        var _this = this;
        if(!_this._isEdit()){
            _this._initShow();
        }
    },
    _initShow:function(value){
        var _this = this;
        _this._loadData(_this._showInit);
    },
    _loadData:function(func){
        var _this = this;
        var apiServer = _this.getOption("apiServer");
        var call = _this.getOption("call");
        var dictCode = _this.getOption("dictCode");
        var idField = _this.getOption("idField");
        var basePath = _this.getOption("basePath");
        var params = _this.getOption("params");
        if(params == null){
            params = {};
        }

        _this.data = [];
        var route = {
            "apiServer":apiServer,
            "call":call,
            "description":"查询下拉框条目"
        };

        var url= basePath + "/callService";
        var options = {
            url:url,
            route:route,
            data:params,
            callback:function(result){
                func.call(_this,result);
            }
        } ;
        $.cc.ajaxDtoRequestCommon(options);
    },
    _editInit:function(result){
        var _this = this;
        var state = result.getState();
        var info = result.getInfo();
        if(state == 0){
            var list = result.get("list");
            _this.data = list;
            var dataFilter = _this.getOption("dataFilter");
            list = dataFilter.call(window,list);

            _this._setData(list);
            var initValue= _this.options["initValue"];
            if(isNotEmpty(_this.value)){
                _this.setValue(_this.value);
            }else if(isNotEmpty(initValue)){
                _this.setValue(initValue);
            }
        }else{
            $.cc.error(info);
        }
        _this._loadSuccess();
    },
    _showInit:function(result){
        var _this = this;
        var state = result.getState();
        var info = result.getInfo();
        if(state == 0){
            var resultKey = _this.getOption("resultKey");
            var list = result.get(resultKey);
            var dataFilter = _this.getOption("dataFilter");
            list = dataFilter.call(window,list);
            _this.data = list;
            var initValue = _this.getOption("initValue");
            if(isNotEmpty(_this.value)){
                _this.setValue(_this.value);
            }else if (isNotEmpty(initValue)){
                _this.setValue(initValue);
            }
        }else{
            $.cc.error(info);
        }
        _this._loadSuccess();
    },
    _setData:function(list){
        var _this = this;

        var multiple = _this.getOption("multiple");
        var idField = _this.getOption("idField");
        var textField = _this.getOption("textField");

        _this.component.empty();
        if(list != null && list.length>0){
            for(var i=0;i<list.length;i++){
                var item = list[i];
                var value = item[idField];
                var name = item[textField];
                var _html='<div class="pretty o-primary"><input type="checkbox" name="checkbox_'+_this.id+'" value="'+value+'" /><label><i class="mdi mdi-check"></i>&nbsp;'+name+'</label></div>';
                _this.component.append(_html);
            }
        }

        var initValue= _this.options["initValue"];
        if(isNotEmpty(_this.value)){
            _this.setValue(_this.value);
        }else if(isNotEmpty(initValue)){
            _this.setValue(initValue);
        }
        _this._bindCheckEvent();
    },
    _bindCheckEvent:function(){
        var _this = this;
        if(_this._isEdit()){
            var event = {
                "name":_this.id
            };
            $(":checkbox",_this.component).unbind("click").bind("click",function(e){

                event['source'] = _this.component;
                event['value'] = _this.getValue();
                _this._clickHandler(event);
            });
        }
    },
    setValue:function(value,change) {
        var _this = this;
        _this.value = value;
        if(isEmpty(change)){
            change = true;
        }

        var idField = _this.getOption("idField");
        var textField = _this.getOption("textField");

        if(isEmpty(value) || isEmpty(_this.data) || _this.data.length == 0){
            _this.value = null
            if(!_this._isEdit()){
                _this.component.html("");
            }
        }else{
            _this.value = value;
            if (_this._isEdit()) {
                $(":checkbox",_this.component).prop("checked",false);
                var values = (value+"").split(",");
                if(values != null && values.length>0){
                    for(var i=0;i<values.length;i++){
                        var val = values[i];
                        $(":checkbox[value='"+val+"']",_this.component).prop("checked",true);
                    }
                }

            } else {
                var values = (value+"").split(",");
                if(values != null && values.length>0){
                    var names=[] ;
                    for(var i=0;i<values.length;i++){
                        var itemValue = values[i];

                        _this.data.each(function(item,i){
                            var id = item[idField];
                            var name = item[textField];
                            if(id == itemValue){
                                names.add(name);
                            }
                        });
                    }
                    if(names.length>0){
                        _this.component.html(names.join(","));
                    }else{
                        _this.value = null;
                        _this.component.html('');
                    }

                }else{
                    _this.value = null;
                    _this.component.html(names.join(","));
                }

            }
        }
        if(change){
            _this._fire("onchange");
        }
    },
    _clickHandler:function(event){
        var _this = this;
        _this._fire("onclick",event);
        var original = isNotEmpty(_this.original)?_this.original:_this.getOption("initValue");
        var value = _this.getValue();
        if(original != value){
            _this.original=value;
            _this._fire("onchange",event);
        }
    },
    _sync:function(){
        var _this = this;
        var idField = _this.getOption("idField");
        var textField = _this.getOption("textField");

        var values = [];
        $(":checkbox",_this.component).each(function(){
            var $checkBox = $(this);
            if($checkBox.is(":checked")){
                var value = $checkBox.val();
                values.add(value);
            }

        });
        _this.value = values.join(",");
    },
    _setStyle:function(){
        var _this = this;
        var width = _this.getOption('width');
        if(isNotEmpty(width)){
            _this.component.css({
                "width":width + "px"
            });
        }
    }
});
